import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';


const APPBAR_SCROLL_OFFSET = 300;

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> with AutomaticKeepAliveClientMixin {

  double _appBarAlpha = 0;
  RefreshController _refreshController = RefreshController(initialRefresh: false);

  _onsroll(offset) {
    setState(() {
      double alpha = offset / 100;
      if(alpha < 0) alpha = 0;
      if(alpha > 1) alpha = 1;

      setState(() {
        _appBarAlpha = alpha;
      });
    });
  }
  void _onRefresh() async{
    // monitor network fetch
    await Future.delayed(Duration(milliseconds: 1000));
    // if failed,use refreshFailed()
    _refreshController.refreshCompleted();
  }

  void _onLoading() async{
    // monitor network fetch
    await Future.delayed(Duration(milliseconds: 1000));
    // if failed,use loadFailed(),if no data return,use LoadNodata()
    if(mounted)
    setState(() {

    });
    _refreshController.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
        children: <Widget>[
          MediaQuery.removePadding(
            removeTop: true,
            context: context,
            child: Scaffold(
              body: NotificationListener(
                onNotification: (scrollNotification){
                  if(scrollNotification is ScrollUpdateNotification && scrollNotification.depth == 0){
                    _onsroll(scrollNotification.metrics.pixels);
                  }
                },
                child: SmartRefresher(
                enablePullDown: true,
                enablePullUp: true,
                header: WaterDropHeader(),
                footer: CustomFooter(
                    builder: (BuildContext context,LoadStatus mode){
                      Widget body ;
                      if(mode==LoadStatus.idle){
                        body =  Text("pull up load");
                      }
                      else if(mode==LoadStatus.loading){
                        body = CupertinoActivityIndicator();
                      }
                      else if(mode == LoadStatus.failed){
                        body = Text("Load Failed!Click retry!");
                      }
                      else if(mode == LoadStatus.canLoading){
                          body = Text("release to load more");
                      }
                      else{
                        body = Text("No more Data");
                      }
                      return Container(
                        height: 55.0,
                        child: Center(child:body),
                      );
                    },
                  ),
                controller: _refreshController,
                onLoading: _onLoading,
                onRefresh: _onRefresh,
                child: ListView.builder(
                    itemCount: 60,
                    itemBuilder: (context, index){
                      return Container(
                        padding: const EdgeInsets.all(16),
                        child: Text("this is $index"),
                      );
                    },
                  ),
                ),
              ),
            ),
          ),
          Opacity(
            opacity: _appBarAlpha,
            child: Container(
              height: 80,
              decoration: BoxDecoration(color: Colors.white),
              child: Center(
                child: Padding(
                  padding: EdgeInsets.only(top: 40),
                  child: Text("搜索"),
                ),
              ),
            ),
          )
        ],
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}